<script setup>
import {
  getUser,
  addUser,
  updateUser,
  checkUserNameUnique
} from "@/api/system/user";

import {getJobInfo} from "@/api/monitor/job"

const props = defineProps({
  id: {
    type: [Number, String],
    default: ""
  }
});

const formRef = ref();
const data = ref({
  loading: false,
  form: {
    id: props.id,
  },
  rules: {
    jobName: [{ required: true, message: "请输入任务名称", trigger: "blur" }],
    jobGroup: [{ required: true, message: "请选择任务分组", trigger: "blur" }],
    invokeTarget: [{ required: true, message: "请输入调用方法", trigger: "blur" }],
    cronExpression: [{ required: true, message: "请输入cron表达式", trigger: "blur" }],
  }
});

onMounted(() => {
  if (data.value.form.id != "" ) {
    getInfo();
  }
});

// 编辑获取信息
function getInfo() {
  data.value.loading = true;
  getJobInfo(props.id).then((res) => {
    data.value.loading = false;
    data.value.form = res.data;
  });
}

function getcheckUserNameUnique(rule, value, callback) {
  const msg = "登陆账号已存在";
  if (value === "") {
    callback();
  } else {
    const checkData = {
      userName: value
    };
    checkUserNameUnique(checkData).then((response) => {
      if (response.data.code === "1") {
        callback();
      } else {
        callback(msg);
      }
    });
  }
}
defineExpose({
  submit(callback) {
    if (data.value.form.id == "") {
      formRef.value.validate((valid) => {
        if (valid) {
          addUser(data.value.form).then(() => {
            ElMessage.success({
              message: "保存成功",
              center: true
            });
            callback && callback();
          });
        }
      });
    } else {
      formRef.value.validate((valid) => {
        if (valid) {
          updateUser(data.value.form).then(() => {
            ElMessage.success({
              message: "修改成功",
              center: true
            });
            callback && callback();
          });
        }
      });
    }
  }
});
</script>

<template>
  <div v-loading="data.loading">
    <el-form
      ref="formRef"
      :model="data.form"
      :rules="data.rules"
      label-width="120px"
      label-suffix=" "
      label-position="top"
      style="padding: 0 20px"
    >
      <el-row :gutter="30">
        <el-col :span="24">
          <el-form-item label="任务名称" prop="jobName"> 
            <el-input  v-model="data.form.jobName" placeholder="请输入任务名称" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="任务分组" prop="jobGroup">
            <el-select v-model="data.form.jobGroup"  placeholder="请选择任务分组" style="width:100%">
              <el-option
                key="item.value"
                label="item.label"
                value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="调用方法" prop="invokeTarget">
            <el-input  placeholder="请选择调用方法" v-model="data.form.invokeTarget"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="cron表达式" prop="cronExpression">
            <el-input v-model="data.form.cronExpression"  placeholder="请选择cron表达式" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="是否并发" prop="concurrent" >
            <el-radio-group v-model="data.form.concurrent">
              <el-radio-button  label="0" >允许</el-radio-button>
              <el-radio-button  label="1"  >禁止</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="错误策略" prop="misfirePolicy">
            <el-radio-group v-model="data.form.misfirePolicy">
              <el-radio-button  label="1" >立即执行</el-radio-button>
              <el-radio-button  label="2"  >执行一次</el-radio-button>
              <el-radio-button  label="3"  >放弃执行</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="状态" prop="status" >
            <el-radio-group v-model="data.form.status">
              <el-radio label="0">正常</el-radio>
              <el-radio label="1">暂停</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="data.form.remark" :rows="2" type="textarea" placeholder="请输入备注" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
